package beta;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MinUICanvas extends Canvas
{
    public static final int SCREEN    = 1;
    public static final int TITLE_BAR = 2;
    public static final int MENU_BAR  = 3;
    public static final int ALL       = 4;
    
    
    private boolean leftMenuPressed = false;
    private boolean rightMenuPressed = false;
    
    private int screenWidth;
    private int screenHeight;

    public MinUICanvas()
    {
        setFullScreenMode(true);
    }

    public int getScreenHeight()
    {
        return screenHeight;
    }

    public int getScreenWidth()
    {
        return screenWidth;
    }
    
    public void update()
    {
        Theme theme = ScreenManager.getCurrentTheme();
        int height = getHeight();
        
        screenWidth = getWidth();
        if(ScreenManager.getCurrentScreen() == null)
        {
            System.out.println("fullscreen + screen == null");
            screenHeight = height;
        }
        else
        {
            if(ScreenManager.isFullScreen())
            {
                System.out.println("fullscreen + screen != null");
                screenHeight = height;
            }
            else
            {
                System.out.println("non fullscreen + screen != null");
                screenHeight = height - theme.getMenuBarHeight() - theme.getTitleHeight() - 2 * theme.getPadding();
                screenWidth -= 2 * theme.getPadding();
            }
        }
    }
    
    public void repaint(int what)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        switch(what)
        {
            case SCREEN:
                repaint(0, theme.getTitleHeight(), getWidth(), getHeight() - theme.getMenuBarHeight() - theme.getTitleHeight());
                break;
            case TITLE_BAR:
                repaint(0, 0, getWidth(), theme.getTitleHeight());
                break;
            case MENU_BAR:
                repaint(0, getHeight() - theme.getMenuBarHeight(), getWidth(), getHeight());
                break;
            case ALL:
                repaint();
                break;
        }
    }

    protected void keyPressed(int key)
    {
        Screen screen = ScreenManager.getCurrentScreen();
        if(screen == null)
            return;
        if(key == Screen.LEFT_SOFTKEY)
        {
            leftMenuPressed = true;
            repaint(MENU_BAR);
            screen.onCancel();
        }
        else if(key == Screen.RIGHT_SOFTKEY)
        {
            rightMenuPressed = true;
            repaint(MENU_BAR);
            screen.onOK();
        }
        screen.onKeyPressed(key);
    }

    protected void keyReleased(int key)
    {
        if(key == Screen.LEFT_SOFTKEY || key == Screen.RIGHT_SOFTKEY)
        {
            leftMenuPressed = rightMenuPressed = false;
            repaint(MENU_BAR);
        }
        ScreenManager.getCurrentScreen().onKeyReleased(key);
    }

    protected void keyRepeated(int key)
    {
        ScreenManager.getCurrentScreen().onKeyRepeated(key);
    }

    protected void paint(Graphics g)
    {
        Theme theme = ScreenManager.getCurrentTheme();
        Screen screen = ScreenManager.getCurrentScreen();
        int width = getWidth();
        int height = getHeight();
        int temp1, temp2;
        
        if(screen == null)
        {
            theme.drawNullScreen(g, screenWidth, screenHeight);
            return;
        }
        if(ScreenManager.fullScreen)
        {
            theme.drawScreenBackground(g, screenWidth, screenHeight);
            screen.paint(g, screenWidth, screenHeight);
        }
        else
        {
            temp1 = theme.getTitleHeight();
            temp2 = height - temp1 - theme.getMenuBarHeight();
            
            g.setClip(0, 0, width, temp1);
            theme.drawTitle(g, screen.getTitle(), width, temp1);
            
            g.translate(0, temp1);
            g.setClip(0, 0, width, temp2);
            theme.drawScreenBackground(g, width, temp2);
            
            temp1 = theme.getPadding();
            g.translate(temp1, temp1);
            g.setClip(0, 0, screenWidth, screenHeight);
            screen.paint(g, screenWidth, screenHeight);
            
            g.translate(-temp1, screenHeight + temp1);
            temp1 = theme.getMenuBarHeight();
            g.setClip(0, 0, width, temp1);
            theme.drawMenuBar(g, 
                              screen.getLeftMenu(), leftMenuPressed, 
                              screen.getRightMenu(), rightMenuPressed, 
                              width, temp1);
        }
    }
    
}
